//
//  ShopGoodViewController.m
//  BaseFrame
//
//  Created by 孟学浩 on 2017/5/18.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "ShopGoodViewController.h"
#import "ShopGoodCollectionViewCell.h"
#import "ShopGoodInfoViewController.h"
#import "PulldownView.h"

@interface ShopGoodViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) NSMutableArray *dataArray;

@property (nonatomic,strong) UIView *titleView;
@property (nonatomic,strong) UIButton *titleButton;

@property (nonatomic,strong) PulldownView *pulldownView;

@end

//网络
@implementation ShopGoodViewController (NetWorking)

- (void)getUpdStoreFire:(UIImage *)image{
    [self showprogressHUD];
    if ([[UserModel sharedInstanced].s_id isEqualToString:@""]) {
        [self hiddenProgressHUD];
        return;
    }
    NSDictionary *parameters = @{@"s_id":[UserModel sharedInstanced].s_id};
    [[AFAppDotNetAPIClient sharedClient] POST:@"StoreMobile/updStoreFire.action" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        NSData *data = UIImageJPEGRepresentation(image, .1);
        NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
        [formData appendPartWithFileData:data name:@"file" fileName:[NSString stringWithFormat:@"%ld.jpg",(NSUInteger)time] mimeType:@"image/jpeg"];
        
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"ResponseStatus"] isEqualToString:@"0"]) {
            [self getData];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
        [self hiddenProgressHUD];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self showError:error];
        [self hiddenProgressHUD];
    }];
}

- (void)getData{
    [self showprogressHUD];
    if ([[UserModel sharedInstanced].s_id isEqualToString:@""]) {
        [self hiddenProgressHUD];
        return;
    }
    NSDictionary *parameters = @{@"s_id":[UserModel sharedInstanced].s_id};
    [[AFAppDotNetAPIClient sharedClient] POST:@"Commodity/selectBysId.action" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"ResponseStatus"] isEqualToString:@"0"]) {

            [self.dataArray removeAllObjects];
            [self.dataArray addObjectsFromArray:responseObject[@"selectBySiD"]];
            [self.collectionView reloadData];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

@end

@implementation ShopGoodViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.navigationItem.title = @"商品列表";
    self.navigationItem.titleView = self.titleView;
    [self.titleView addSubview:self.titleButton];
    [self.view addSubview:self.collectionView];
    
    UIButton * rightButton = [[UIButton alloc]init];
    rightButton.size = CGSizeMake(40, 20);
    [rightButton setTitle:@"添加" forState:UIControlStateNormal];
    [rightButton.titleLabel setFont:DEF_FontSize_12];
    rightButton.layer.masksToBounds = YES;
    rightButton.layer.cornerRadius = 3;
    rightButton.layer.borderColor = [[UIColor whiteColor] CGColor];
    rightButton.layer.borderWidth = 1;
    [rightButton addTarget:self action:@selector(rightBarButtonItemClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    //设置barbutton
    self.navigationItem.rightBarButtonItem = rightItem;
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self getData];
}

#pragma mark - click

- (void)rightBarButtonItemClick{
    ShopGoodInfoViewController *vc = [ShopGoodInfoViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    vc.type = ShopGoodsType_Add;
    [self.navigationController pushViewController:vc animated:YES];
}
// 点击标题按钮选择分类
- (void)chooseShopType{
    [self.pulldownView showInSuperView:self.view];
}
#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
//    return self.dataArray.count;
    return 10;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ShopGoodCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ShopGoodCollectionViewCell class]) forIndexPath:indexPath];
//    cell.model = self.dataArray[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    ShopGoodInfoViewController *vc = [ShopGoodInfoViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    vc.type = ShopGoodsType_Change;
//    vc.c_id = self.dataArray[indexPath.row][@"c_id"];
    [self.navigationController pushViewController:vc animated:YES];
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *view = nil;
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([UICollectionReusableView class]) forIndexPath:indexPath];
        view.backgroundColor = [UIColor whiteColor];
//        [view addSubview:self.imgView];
    }
    return view;
}

- (void)imgViewHeadViewCkick{
    [self selectPhotoAlbumWithSelectPhotoHandle:^(UIImage *img) {
        if ([[NSString stringWithFormat:@"%@",[UserModel sharedInstanced].s_id] isEqualToString:@""]) {
            ShowMessage(@"请先创建店铺");
            return ;
        }
        [self getUpdStoreFire:img];
    }];
}

#pragma mark - set & get
- (UIView *)titleView{
    if(!_titleView){
        _titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80/WIDTH_5S_SCALE, 25/WIDTH_5S_SCALE)];
        
        UIImageView *subscript = [[UIImageView alloc] initWithFrame:CGRectMake(70/WIDTH_5S_SCALE, 10/WIDTH_5S_SCALE, 10/WIDTH_5S_SCALE, 10/WIDTH_5S_SCALE)];
        subscript.image = [UIImage imageNamed:@"三角下标"];
//        subscript.backgroundColor = [UIColor redColor];
        [_titleView addSubview:subscript];
        
    }
    return _titleView;
}

- (UIButton *)titleButton{
    if(!_titleButton){
        _titleButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70/WIDTH_5S_SCALE, 25/WIDTH_5S_SCALE)];
        _titleButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
        [_titleButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_titleButton setTitle:@"请选择分类" forState:UIControlStateNormal];
        [_titleButton addTarget:self action:@selector(chooseShopType) forControlEvents:UIControlEventTouchUpInside];
    }
    return _titleButton;
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        
        int offset = 5;
        
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        layout.minimumLineSpacing = offset;
        layout.minimumInteritemSpacing = offset;
        layout.sectionInset = UIEdgeInsetsMake(offset, offset, offset, offset);
        layout.itemSize = CGSizeMake((SCREEN_WIDTH - 20)/3., (SCREEN_WIDTH - 20)/3.);
//        layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 160/WIDTH_5S_SCALE);

        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64-49) collectionViewLayout:layout];
        _collectionView.backgroundColor = getColor(@"f1f2f6");
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[ShopGoodCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([ShopGoodCollectionViewCell class])];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([UICollectionReusableView class])];
    }
    return _collectionView;
}

- (PulldownView *)pulldownView{
    if(!_pulldownView){
        _pulldownView = [[PulldownView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49)];
        _pulldownView.dataArray = @[@"蔬菜类",@"水果类",@"海鲜类",@"服装类",@"餐饮类",@"家具类"].mutableCopy;
        _pulldownView.clickedAtIndexBlock = ^(NSInteger *index) {
            NSLog(@"点击了第%ld行",index);
            [self.titleButton setTitle:[NSString stringWithFormat:@"第%d行",index] forState:UIControlStateNormal];
        };
    }
    return _pulldownView;
}
#pragma mark - data

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray  = @[
                        ].mutableCopy;
    }
    return _dataArray;
}


@end
